#include "LUA_util.h"

void* createClass(lua_State* L,size_t sz, char* libName){
	luaL_checktype(L, 1, LUA_TTABLE); 
	lua_newtable(L);
	lua_pushvalue(L,1);       
    lua_setmetatable(L, -2);
	lua_pushvalue(L,1);
    lua_setfield(L, 1, "__index");

	void* pointer = lua_newuserdata(L, sz);	
	
	lua_getglobal(L, libName);	
	lua_setmetatable(L, -2);
	lua_setfield(L, -2, "__self");

	return pointer;
}

void* getClass(lua_State* L, char* libName){
	luaL_checktype(L, 1, LUA_TTABLE);
	lua_getfield(L, 1, "__self");
	void* pointer = lua_touserdata(L, -1);
	luaL_argcheck(L, pointer!=0,-1, "'userdata' expected");
	return pointer;
}